package com.example.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.dto.ArticleDetailDTO;
import com.example.entity.Archives;
import com.example.handlers.BaseContext;
import com.example.service.ArchivesService;
import com.example.vo.ArticleDetailVO;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController("userArchivesController")
@RequestMapping("user/archives")
public class ArchivesController {

    @Autowired
    private ArchivesService archivesService;

    /**
     * 获取归档列表，支持按年份、月份、分类过滤，并分页排序
     *
     * @return
     */
    @GetMapping("/list")
    public Result<Page<Archives>> listArchives(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) String month,
            @RequestParam(required = false) String category,
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long pageSize
    ) {
        Page<Archives> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Archives> queryWrapper = new LambdaQueryWrapper<>();
        // 获取线程变量
        Integer userId = BaseContext.getCurrentId();
        queryWrapper.eq(Archives::getUserId, userId);

        if (year != null) {
            queryWrapper.eq(Archives::getYear, year);
        }
        if (month != null && !month.isEmpty()) {
            queryWrapper.eq(Archives::getMonth, month);
        }
        if (category != null && !category.isEmpty()) {
            queryWrapper.like(Archives::getCategory, category); // 修改为模糊查询
        }

        queryWrapper.orderByDesc(Archives::getCreatedAt);
        archivesService.page(pageInfo, queryWrapper);

        return Result.success(pageInfo);
    }

    /**
     * 删除归档
     *
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<Object> deleteArchive(@PathVariable Integer id) {
        archivesService.removeById(id);
        return Result.success();
    }

}


